const {ipcMain,BrowserWindow} = require('electron')
const path = require("path");
const saveas = require('../controller/saveas')
const cssText = `cursor:pointer;width:60px;height:30px;background-color:red;border-radius:5px;text-align:center;line-height:30px;position:fixed;bottom:50px;right:30px;z-index:1000;color:black;`
let win = null
const js = `
    const div = document.createElement('div')
    div.innerHTML = '关闭窗口'
    div.style.cssText = '${cssText}'
    div.addEventListener('click',()=>{
        myApi.close()
    })
    document.body.appendChild(div)
`

ipcMain.handle('on-open-event',(e,url)=>{

    win = new BrowserWindow({
        width:1200,
        height:800,
        webPreferences:{
            preload:path.resolve(__dirname,'../preload/open.js')
        }
    })

    win.loadURL(url)
    win.webContents.openDevTools()
    win.webContents.executeJavaScript(js).catch(()=>{})
    win.webContents.on('context-menu',(e,args)=>{
        saveas(args.srcURL)
    })
})

ipcMain.handle('on-close-event',e=>{
    win.close()
})
